package com.gooluke.redismap.entity;

import lombok.Data;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author gooluke
 * description
 * datetime 2025-09-14 17:01
 */
@Data
public class StringRedisData {

    private final String value;
    private final long expireTime; // 过期时间戳（毫秒），0表示永不过期
    private final AtomicLong accessCount = new AtomicLong(0);
    private final long createTime;

    public StringRedisData(String value) {
        this(value, 0);
    }

    public StringRedisData(String value, long expireTime) {
        this.value = value;
        this.expireTime = expireTime;
        this.createTime = System.currentTimeMillis();
    }

    public String getValue() {
        accessCount.incrementAndGet();
        return value;
    }

    public boolean isExpired() {
        return expireTime > 0 && System.currentTimeMillis() > expireTime;
    }

    public long getAccessCount() {
        return accessCount.get();
    }

    public long getTtl() {
        if (expireTime == 0) {
            return -1; // 永不过期
        }
        long ttl = (expireTime - System.currentTimeMillis()) / 1000;
        return ttl > 0 ? ttl : -2; // -2表示已过期
    }

}
